package com.game.modular.game.playerMatch.service;

import com.game.modular.game.playerAssets.domain.PlayerHero;
import com.game.modular.game.playerAssets.mapper.AccountRanksMapper;
import com.game.modular.game.playerMatch.domain.PvpCity;
import com.game.modular.game.playerMatch.service.base.PvpNpcRanksBaseServiceImpl;
import com.game.modular.gameCore.cultivate.domain.CoreHero;
import com.game.modular.gameCore.cultivate.domain.CoreHeroStar;
import com.game.modular.gameCore.cultivate.domain.CoreSkillStar;
import com.game.modular.gameCore.cultivate.service.impl.CoreHeroServiceImpl;
import com.game.modular.gameCore.cultivate.service.impl.CoreHeroStarServiceImpl;
import com.game.modular.gameCore.cultivate.service.impl.CoreSkillStarServiceImpl;
import com.game.modular.gameCore.map.service.impl.CoreCityServiceImpl;
import com.game.modular.gameCore.npc.domain.CoreNpc;
import com.game.modular.gameCore.npc.domain.CoreNpcRanks;
import com.game.modular.gameCore.npc.service.impl.CoreNpcRanksServiceImpl;
import com.game.modular.gameCore.npc.service.impl.CoreNpcServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 武将队伍(PvpRanks)表服务实现类
 *
 * @author cxy
 * @since 2024-07-18 11:19:32
 */
@Validated
@Slf4j
@Service
public class PvpNpcRanksServiceImpl extends PvpNpcRanksBaseServiceImpl {
    @Resource
    private AccountRanksMapper accountRanksMapper;
    @Resource
    private CoreHeroServiceImpl coreHeroService;
    @Resource
    private PvpCityServiceImpl pvpCityServiceImpl;
    @Resource
    private CoreNpcServiceImpl coreNpcServiceImpl;
    @Resource
    private CoreNpcRanksServiceImpl coreNpcRanksServiceImpl;
    @Resource
    private ValueOperations<Object, Object> valueOperations;
    @Resource
    private CoreHeroStarServiceImpl coreHeroStarService;
    @Resource
    private CoreCityServiceImpl coreCityService;
    @Autowired
    private CoreSkillStarServiceImpl coreSkillStarServiceImpl;

    /**
     * 初始化 npc 守备军
     */
    public void init(Long matchId, Long coreCityId) {
        List<PlayerHero> defenseSide = new ArrayList<>();
        Long npcArmyId = coreCityService.getCache(coreCityId).getNpcArmy();
        CoreNpcRanks npcRanks = coreNpcRanksServiceImpl.getCache(npcArmyId);
        AtomicInteger atomicIndex = new AtomicInteger(1);
        npcRanks.getNpcHeroMap().forEach((npcId, troops) -> {
            int rankOrder = atomicIndex.getAndIncrement();
            CoreNpc coreNpc = coreNpcServiceImpl.getCache(npcId);
            CoreHero coreHero = coreHeroService.getCache(npcId);
            CoreHeroStar coreHeroStar = coreHeroStarService.getCache(coreHero.getId(), coreNpc.getStar());

            PlayerHero npc = new PlayerHero();

            npc.setId(coreNpc.getId());
            npc.setCoreHeroId(npcId);
            npc.setLevel(coreNpc.getLevel());
            npc.setStar(coreNpc.getStar());
            npc.setRankOrder(rankOrder);

            npc.setUid(0L);
            npc.setName("npc:"+ coreHero.getName());
            PlayerHero.recalculateAttributes(npc, coreHero, coreHeroStar);
            npc.setRankCamp(0);
            npc.setCurrentForce(PlayerHero.getMaxCurrentForce(npc));

            CoreSkillStar coreSkillStar = coreSkillStarServiceImpl.getCache(coreHero.getSkill(), npc.getStar());
            npc.setSkill(coreSkillStar.getId());

            npc.setSkillSlot1(coreNpc.getSkillSlot1());
            npc.setSkillSlot2(coreNpc.getSkillSlot2());

            defenseSide.add(npc);
        });

        savaCache(matchId,coreCityId,defenseSide);
    }


    public List<PlayerHero> getNpcCityHero(Long matchId, Long coreCityId)  {
        return selectCache(matchId, coreCityId);
    }
}
